package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.moban.PageBean;
import com.kecheng.model.TeachClassify;
import com.kecheng.pojo.KeChengClassify;
import com.kecheng.pojo.Teacher;
import com.kecheng.pojo.TeacherSkill;
import com.kecheng.util.FileUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * 后台教师模块
 * @author
 */
@ParentPackage("json-default")
@Action(value = "teacher", results = {
        @Result(name = "teacherList", location = "/admin/teacher/teacherList.jsp"),
        @Result(name = "toDetail", location = "/admin/teacher/detail.jsp"),
        @Result(name = "redirectList", type = "redirect",location = "teacher!teacherList.action"),
        @Result(name = "redirectDetail", type = "redirect",location = "teacher!toDetail.action",params = {"teacher","teacher"}),
        @Result(name = "modifyJn", type = "json",params = {"root","object"})
})
@AllowedMethods(value = {("regex:.*")})
public class TeacherAction extends BaseAction {
    @Autowired
    CommonDao dao;

    private Teacher teacher;
    private List<Integer> id;
    private List<KeChengClassify>classifyList;
    private PageBean<Teacher> pageBean;
    private TeachClassify teachClassify;
    private File file;
    private String fileFileName;


    /**
     * 教师列表
     * @return
     */
    public String teacherList(){
        pageBean = (PageBean<Teacher>) dao.findPageBean(Teacher.class,pageBean);
        return "teacherList";
    }
    /**
     *教师详情页
     * @return
     */
    public String toDetail(){
        if(teacher!=null)teacher = (Teacher)dao.findOne(teacher);
        //获取技能信息，以及教师拥有的技能信息
        KeChengClassify classify = new KeChengClassify();
        classify.setParentId(0);
        classifyList = (List<KeChengClassify>)dao.find(classify);
        List<KeChengClassify> sonList = null;
        for(KeChengClassify kc:classifyList){
            classify = new KeChengClassify();
            classify.setParentId(kc.getId());
            sonList = (List<KeChengClassify>)dao.find(classify);
            if(teacher!=null){
                for (KeChengClassify son:sonList){
                    TeacherSkill teacherSkill = new TeacherSkill();
                    teacherSkill.setClassifyId(son.getId());
                    teacherSkill.setTeacherId(teacher.getId());
                    teacherSkill = (TeacherSkill)dao.findOne(teacherSkill,new String[]{"teacherId","classifyId"});
                    if (teacherSkill!=null){
                        son.setHasChoose(true);
                    }
                }
            }
            kc.setSonList(sonList);
        }
        return "toDetail";
    }

    /**
     * 更新或者添加
     */
    @Transactional(readOnly = false)
    public String save(){
        if(teacher.getId()!=null){
            if(file!=null)teacher.setImage(FileUtil.pictureUtil(file,fileFileName,root));
            dao.update(teacher);
        }else{
            teacher.setImage(FileUtil.pictureUtil(file,fileFileName,root));
            if(teacher.getHasTeachNum()==null)teacher.setHasTeachNum(0);//授课数量，没有添加就给一个默认值0
            dao.add(teacher);
        }
        return toDetail();
    }

    /**
     *删除
     * @return
     */
    @Transactional(readOnly = false)
    public String delete(){
        if(teacher!=null)dao.delete(teacher);
        else if(id!=null){
            for(Integer i:id){
                teacher = new Teacher();
                teacher.setId(i);
                dao.delete(teacher);
            }
        }
        return "redirectList";
    }

    /**
     * 更新老师的技能
     * @return
     */
    @Transactional(readOnly = false)
    public String modifyJn(){
        if (teachClassify.isFlag()){
            //增加技能
            TeacherSkill teacherSkill = new TeacherSkill();
            BeanUtils.copyProperties(teachClassify,teacherSkill);
            teacherSkill.setLevel(1);//这里暂时默认给一个值
            dao.add(teacherSkill);
        }else{
            //删除技能
            TeacherSkill teacherSkill = new TeacherSkill();
            BeanUtils.copyProperties(teachClassify,teacherSkill);
            teacherSkill = (TeacherSkill)dao.findOne(teacherSkill,new String[]{"classifyId","teacherId"});
            dao.delete(teacherSkill);
        }
        return "modifyJn";
    }

    public PageBean<Teacher> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<Teacher> pageBean) {
        this.pageBean = pageBean;
    }


    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public List<Integer> getId() {
        return id;
    }

    public void setId(List<Integer> id) {
        this.id = id;
    }

    public List<KeChengClassify> getClassifyList() {
        return classifyList;
    }

    public void setClassifyList(List<KeChengClassify> classifyList) {
        this.classifyList = classifyList;
    }

    public TeachClassify getTeachClassify() {
        return teachClassify;
    }

    public void setTeachClassify(TeachClassify teachClassify) {
        this.teachClassify = teachClassify;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
}
